﻿ using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using IDAL;
using MODEL;

namespace DAL
{
    public class DBContextFactory : IDBContextFactory
    {
        #region  创建 EF上下文 对象，在线程中共享 一个 上下文对象  +DbContext GetDbContext()
        public DbContext GetDbContext(string type)
        {
            DbContext DBContext = CallContext.GetData(type) as DbContext;
            if (DBContext == null)
            {
                switch (type)
                { 
                 case "AccountInfo": 
                 case "AmazonAD": 
                 case "AmazonAdProperty": 
                 case "AreaCarriersDeliveryMethod": 
                 case "AreaCarriersDeliveryMethodChargeType": 
                 case "AreaCarriersDeliveryMethodContraband": 
                 case "AreaCarriersDeliveryMethodPackage": 
                 case "AuthorizedInfoConfig": 
                 case "BrowseNode": 
                 case "Carrier": 
                 case "CurrencyConvertLoss": 
                 case "CustomsRateInfo": 
                 case "DeliveryMethod": 
                 case "DistributeArea": 
                 case "DistributeAreaStock": 
                 case "GrossProfitConfig": 
                 case "ItemOpreationCommand": 
                 case "ItemOpreationStatu": 
                 case "LanguageType": 
                 case "ListingFeeInfo": 
                 case "LoginAccountInfo": 
                 case "LoginSellerAccountInfo": 
                 case "LogisticsCountry": 
                 case "LogisticsInfoConfig": 
                 case "PaypalFee": 
                 case "Product": 
                 case "ProductCategory": 
                 case "ProductConfig": 
                 case "ProductCost": 
                 case "ProductEAN": 
                 case "ProductPackageInfo": 
                 case "ProductPicture": 
                 case "ProductProperty": 
                 case "ProductSpec": 
                 case "ProductStatu": 
                 case "ProductTranslation": 
                 case "ProductType": 
                 case "RateOfExchange": 
                 case "RateOfExchangeAnalysi": 
                 case "RateOfExchangeHistory": 
                 case "SalesFinalFee": 
                 case "SalesFinalFeeSection": 
                 case "SalesPlatform": 
                 case "SalesPlatformSiteCurrency": 
                 case "SalesPlatformSiteInfo": 
                 case "SalesPlatformSiteInfoCategory": 
                 case "SalesPlatformSiteInfoCategorySpecific": 
                 case "SalesPlatformSiteInfoLanguageType": 
                 case "SalesPlatformSiteSellerAccountInfo": 
                 case "SalesPlatformSiteSellerAccountInfoTemplateWebPageTemplateTerm": 
                 case "SellerAccountInfo": 
                 case "SiteInfo": 
                 case "SiteSpecRelation": 
                 case "Specific": 
                 case "SpecificsValue": 
                 case "Stock": 
                 case "Template": 
                 case "TemplateProperty": 
                 case "TemplateTerm": 
                 case "TemplateTermsDetail": 
                 case "TemplateWebPage": 
                 case "TransportCharge": 
                 case "TransportType": 
                 case "V_ProductRespones": 
                 case "Value": 
                 case "VATFeeInfo": 
                 case "View_AmazonBrowseNode": 
                 case "View_Category": 
                 case "View_CategorySpecifics": 
                 case "View_Contraband": 
                 case "View_Currency": 
                 case "View_DsSystemMenu": 
                 case "View_Listing": 
                 case "View_Measure": 
                 case "View_Permission": 
                 case "View_ProductSpec": 
                 case "View_ProductStatus": 
                 case "View_Specifics": 
                 case "View_SpecificsValue": 
                 case "View_Value": 
				DBContext = new ADSystemEntities();break; 
				default : DBContext = new ADSystemEntities(); break;
                }
                DBContext.Configuration.ValidateOnSaveEnabled = false;
                CallContext.SetData(type, DBContext);
            }
            return DBContext;
        } 
        #endregion
    }

    public class DBSessionFactory:IDBSessionFactory
    {
        #region 在线程中 共用一个 DBSession 对象
        /// <summary>
        /// 在线程中 共用一个 DBSession 对象
        /// </summary>
        /// <returns></returns>
        public IDBSession GetDBSession()
        {
            //从当前线程中  获得DBContext    数据仓储对象
            IDBSession DBSession = CallContext.GetData(typeof(DBSessionFactory).Name) as DBSession;
            if (DBSession==null)
            {
                DBSession = new DBSession();
                CallContext.SetData(typeof(DBSessionFactory).Name, DBSession);
            }
            return DBSession;
        }
        #endregion
    }
}